<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Webservice
 *
 * @author bonfiglio
 */
class Webservice {

    public $url = false;
    private $datas;
    private $code;
    private $post = true;
    private $header = false;
    private $result = false;
    private $webservice_host = false;
    protected $webservice_hosts = array(
        'DEV' => 'http://extra.certinergy.com/testleads/', //'http://extra.certinergy.com/testleads/',
        'PROD' => 'http://extra.certinergy.com/4dlink/'
    );
    //pas un bonne idée ....
    private $services = array(
        'trombinoscope2' => 'cny_trombinoscope2', //trombinoscope2,
        'getForm' => 'getForm', //Test avec Vincent
        'cny_pacte_energie_solidarite' => 'cny_pacte_energie_solidarite', //Pacte preacrite energie solidarite
        'CertiWSDL' => 'CertiWSDL',
        'WSDL.xml' => 'WSDL.xml'
    );

    public function __construct($service = null, $datas = array(), $post = false, $header = false) {

        $this->setProperty($service, $datas, $post, $header);

        if ($this->url) {

            $this->send();
        }
    }
    public function setProperty($service = null, $datas = array(), $post = false, $header = false) {
        $this->url = $this->getWebserviceHost($service);
        if (!$this->url && ( (boolean) parse_url($service) )) {
            $this->url = $service;
        }

        if ($this->url) {
            $this->datas = $datas;

            $this->post = (int) $post;

            if (!is_array($header)) {
                $header = array('Content-type: application/x-www-form-urlencoded;charset=UTF-8');
            }
            $this->header = $header;
        }
        return $this;
    }

    public function getWebserviceHost($service, $forceProd = false) {
        $this->webservice_host = ((!defined('DEBUG') || DEBUG == 1) && !$forceProd ) ? $this->webservice_hosts['DEV'] : $this->webservice_hosts['PROD'];
        if (isset($this->services[$service])) {
            return $this->webservice_host . $this->services[$service];
        }
        return false;
    }

    public function send() {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_POST, $this->post);

        $url = $this->url;

        if ($this->post) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $this->datas);
        } else if (is_array($this->datas)) {
            $qm = (substr($this->url, -1) == '?') ? '' : '?';
            $url .= $qm . http_build_query($this->datas);
        } elseif (!$this->post) {
            $qm = (substr($this->url, -1) == '/') ? '' : '/';
            $url .= $qm . $this->datas;
        }
        curl_setopt($ch, CURLOPT_URL, $url);

        if ($this->header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        // Send the request
        $this->result = curl_exec($ch);

        $this->code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        // Check the response
        if ($this->result === FALSE) {
            throw new Exception('cURL error: ' . curl_error($ch));
        }
        curl_close($ch);
        return $this;
    }

    public function getJSON($array = false) {
        if ($this->result) {
            return json_decode($this->result, $array);
        }
        return false;
    }

    public function get() {
        return $this->result;
    }

    public function getCodeHTTP() {
        return $this->code;
    }

    public function isAlive() {
        return ($this->code == 200);
    }
    
    public function getHost($prod = true){
        return $this->webservice_hosts[$prod ? 'PROD':'DEV'];
    }

}

?>
